package com.imu.wys.listener;

import com.imu.wys.model.User;

import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.annotation.WebListener;
import javax.servlet.http.HttpSessionAttributeListener;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;
import javax.servlet.http.HttpSessionBindingEvent;
import java.util.ArrayList;
import java.util.List;

@WebListener()
public class UserListener implements ServletContextListener,
        HttpSessionListener, HttpSessionAttributeListener {

    ServletContext context  = null;
    // Public constructor is required by servlet spec
    public UserListener() {
    }

    // -------------------------------------------------------
    // ServletContextListener implementation
    // -------------------------------------------------------
    public void contextInitialized(ServletContextEvent sce) {
      /* This method is called when the servlet context is
         initialized(when the Web application is deployed). 
         You can initialize servlet context related data here.
      */

      context = sce.getServletContext();
      List<String> onlineUserList = new ArrayList<>();
      context.setAttribute("onlineUserList",onlineUserList);
    }

    public void contextDestroyed(ServletContextEvent sce) {
      /* This method is invoked when the Servlet Context 
         (the Web application) is undeployed or 
         Application Server shuts down.
      */
    }

    // -------------------------------------------------------
    // HttpSessionListener implementation
    // -------------------------------------------------------
    public void sessionCreated(HttpSessionEvent se) {
        /* Session is created. */
        context = se.getSession().getServletContext();
        if (context.getAttribute("onlineUsers")!=null){
            context.setAttribute("onlineUsers",(Integer)context.getAttribute("onlineUsers")+1);
        } else {
            context.setAttribute("onlineUsers", 1);
        }
        System.out.println(context.getAttribute("onlineUsers"));
        System.out.println("=====有一位用户上线了=====");
        System.out.println("Id: "+se.getSession().getId()+"=====");

    }


    public void sessionDestroyed(HttpSessionEvent se) {
        /* Session is destroyed. */
        System.out.println("销毁");
        context = se.getSession().getServletContext();
        if (context.getAttribute("onlineUsers")==null){
            context.setAttribute("onlineUsers", 0);
        } else {
            context.setAttribute("onlineUsers",(Integer)context.getAttribute("onlineUsers")-1);
        }
        System.out.println("=====用户下线了=====");
        System.out.println("Id: "+se.getSession().getId()+"=====");
    }

    // -------------------------------------------------------
    // HttpSessionAttributeListener implementation
    // -------------------------------------------------------

    public void attributeAdded(HttpSessionBindingEvent sbe) {
      /* This method is called when an attribute
         is added to a session.
      */
      User user = (User) sbe.getValue();
      context = sbe.getSession().getServletContext();
      List<String> onlineUserList = (List<String>) context.getAttribute("onlineUserList");
      if(!onlineUserList.contains(user.getUsername())){
          onlineUserList.add(user.getUsername());
      }
    }

    public void attributeRemoved(HttpSessionBindingEvent sbe) {
      /* This method is called when an attribute
         is removed from a session.
      */
        User user = (User) sbe.getValue();
        context = sbe.getSession().getServletContext();
        List<String> onlineUserList = (List<String>) context.getAttribute("onlineUserList");
        onlineUserList.remove(user.getUsername());

    }

    public void attributeReplaced(HttpSessionBindingEvent sbe) {
      /* This method is invoked when an attibute
         is replaced in a session.
      */
    }
}
